package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.mapper.BusTeamMapper;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * BusTeam 表数据服务层接口实现类
 *
 */
@Service
public class BusTeamServiceImpl extends SuperServiceImpl<BusTeamMapper, BusTeam,Long> implements IBusTeamService {


    @Resource
    private IAreaTeamService areaTeamService ;


    @Override
    public Page<BusTeam> findBusTeamForManage(DataPermission dataPermission, int pageNo, int pageSize) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        if(dataPermission.getUserType() == 0 || dataPermission.getUserType() == 1 ){
            CriteriaUtils.fillConditionForAreaTeamPermission( criteria,dataPermission.getAreaTeamIds() );
        }else{
            CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds());
        }
        example.orderBy("createTime").desc();
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    @CacheEvict(value = "CacheOfBusTeam",allEntries = true)
    public void saveOrUpdateBusTeam(Long busTeamId, Long areaTeamId, String name) {

        if( busTeamId == null && areaTeamId == null &&name == null  ){
            throw new IllegalArgumentException("Illegal arguments");
        }
        name=name.trim();
        if (StringUtils.isEmpty(name)){
            throw new BusException("名称不能为空");
        }
        if(busTeamId == null){
            if(areaTeamId == null ){
                throw new IllegalArgumentException("Require areaTeamId arguments");
            }
            BusTeam busTeam = new BusTeam();
            busTeam.setAreaTeamId(areaTeamId);
            busTeam.setName(name);
            busTeam.setCreateTime(TimeUtils.now());
            busTeam.setUpdateTime(TimeUtils.now());
            setAreaTeamName(busTeam);
            checkBusTeam(busTeam);
            insertSelective(busTeam);

        }else{

            BusTeam busTeam = new BusTeam();
            busTeam.setBusTeamId(busTeamId);
            if(areaTeamId != null ){
                busTeam.setAreaTeamId(areaTeamId);
                setAreaTeamName(busTeam);
            }
            busTeam.setName(name);
            busTeam.setUpdateTime(TimeUtils.now());
            checkBusTeam(busTeam);
            updateSelectiveById(busTeam) ;

        }


    }

    private void checkBusTeam(BusTeam busTeam) {
        Example<BusTeam> example = BusTeam.createExample();
        example.createCriteria().andEqualTo("name",busTeam.getName());
        BusTeam one = findOneByExample(example);
        if(one != null && (busTeam.getBusTeamId() == null || !busTeam.getBusTeamId().equals(one.getBusTeamId()))){
            throw new BusException("业务组名重复");
        }
    }

    @Override
    @CacheEvict(value = "CacheOfBusTeam",allEntries = true)
    public void deleteBusTeam(Long busTeamId) {
        deleteById(busTeamId);
    }

    @Override
    public List<BusTeam> findBusTeamForList(Long areaTeamId,Long busTeamId) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,areaTeamId,busTeamId);
        return findListByExample(example);
    }

    private void setAreaTeamName(BusTeam busTeam) {
        if(busTeam.getAreaTeamId() != null ){
            busTeam.setAreaTeamName(areaTeamService.findTeamNameById(busTeam.getAreaTeamId()));
        }
    }

    @Override
    public String findTeamNameById(Long busTeamId) {
        Example<BusTeam> example = BusTeam.createExample();
        example.selectProperties("name");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busTeamId",busTeamId);
        BusTeam busTeam = findOneByExample(example);
        return busTeam == null ? StringUtils.EMPTY : busTeam.getName() ;
    }


    @Override
    public List<BusTeam> findBusTeamByAreaTeamIds(List<Long> areaTeamIdList) {

        if(areaTeamIdList == null || areaTeamIdList.isEmpty() ){
            return Collections.EMPTY_LIST ;
        }

        Example<BusTeam> example = BusTeam.createExample();
        example.createCriteria().andIn("areaTeamId" , areaTeamIdList );
        return findListByExample(example);
    }

    @Override
    public List<String> findBusTeamNameByIds(List<Long> busTeamIds) {
        Example<BusTeam> example = BusTeam.createExample();
        example.selectProperties("name");
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("busTeamId",busTeamIds);
        return BeanUtils.toList( findListByExample(example) ,"name" );
    }

    @Override
    public BusTeam findByAreaTeamIdAndBusName(Long areaTeamId, String busTeamName) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name",busTeamName);
        criteria.andEqualTo("areaTeamId",areaTeamId);
        return findOneByExample(example);
    }

    @Override
    public List<BusTeam> findListOrderByAreaTeamId(List<Long> busTeamIds) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("busTeamId",busTeamIds);
        example.orderBy("areaTeamId");
        return findListByExample(example);
    }

    @Override
    @Cacheable(value = "CacheOfBusTeam",key = "#areaTeamId")
    public Map<Long, BusTeam> findBusTeamMapByAreaTeamId(long areaTeamId) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("areaTeamId",areaTeamId);
        List<BusTeam> busTeamList = findListByExample(example);
        return BeanUtils.toMap(busTeamList,"busTeamId");
    }
}